export function humanizeNumber(num) {
  const n = parseInt(num, 10) || 0
  if (n < 1000) return n.toString()
  const decimalCount = n % 1000 === 0 ? 0 : 1  // 整千数不加小数
  const k = n / 1000
  return k.toFixed(decimalCount) + 'k'
}

export function humanizeNumberByWang(num) {
  const n = parseInt(num, 10) || 0
  if (n < 10000) return n.toString()
  const decimalCount = n % 10000 === 0 ? 0 : 1  // 整千数不加小数
  const k = n / 10000
  return k.toFixed(decimalCount) + '万'
}

export function humanizeTime(timeStr) {
  // 解析时间字符串（兼容带空格格式）
  const [datePart, timePart] = timeStr.split(' ');

  const [year, month, day] = datePart.split('-').map(Number);
  const [hours, minutes] = timePart.split(':').map(Number);
  const targetTime = new Date(year, month - 1, day, hours, minutes);

  const now = new Date();
  const diffMs = now - targetTime;

  // 处理未来时间
  if (diffMs < 0) return "刚刚";

  // 计算时间差
  const diffSeconds = Math.floor(diffMs / 1000);
  if (diffSeconds < 60) return "刚刚";

  const diffMinutes = Math.floor(diffMs / (1000 * 60));
  if (diffMinutes < 60) return `${diffMinutes}分钟前`;

  const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
  if (diffHours < 24) return `${diffHours}小时前`;

  const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
  if (diffDays < 4) return `${diffDays}天前`;

  // 格式化日期为 YYYY-MM-DD
  const pad = n => n.toString().padStart(2, '0');
  return `${targetTime.getFullYear()}-${pad(targetTime.getMonth() + 1)}-${pad(targetTime.getDate())}`;
}

export function categorizeTime(timeStr) {
  // 解析时间字符串（兼容带空格格式）
  const [datePart, timePart] = timeStr.split(' ');
  const [year, month, day] = datePart.split('-').map(Number);
  const [hours, minutes] = timePart.split(':').map(Number);
  const targetTime = new Date(year, month - 1, day, hours, minutes);

  // 获取当前日期的 0 点时间（忽略时分秒）
  const now = new Date();
  const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());

  // 获取目标日期的 0 点时间（忽略时分秒）
  const targetDateStart = new Date(targetTime.getFullYear(), targetTime.getMonth(), targetTime.getDate());

  // 计算天数差（精确到日期）
  const diffMs = todayStart - targetDateStart;
  const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));

  // 分类逻辑
  if (diffDays < 0) {
    return "";
  } else if (diffDays === 0) {
    return "今天";
  } else if (diffDays === 1) {
    return "昨天";
  } else if (diffDays <= 6) { // 2-6 天
    return "一周内";
  } else {
    return "更早"; // 7 天及以上
  }
}
